angular.module('mvcApp.shop-list', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/category/:categoryId', {
            templateUrl: 'static/views/shop-list.html',
            controller: 'ShopListCtrl'
        })
        .when('/hotest_shops/:hotest', {
            templateUrl: 'static/views/shop-list.html',
            controller: 'ShopListCtrl'
        });
}]).controller('ShopListCtrl', ['$scope', '$http', '$location', 'User', '$routeParams', 'Shop', 'Category',
    function ($scope, $http, $location, User, $routeParams, Shop, Category) {
        $scope.categoryId = $routeParams.categoryId;
        $scope.isHotest = $routeParams.hotest ? true : false;
        if (!$scope.isHotest) {
            Category.getById($scope.categoryId)
                .then(function (category) {
                    $scope.category = category;
                }).catch(function (err) {
                    alert(err);
                });
        }
        $scope.shops = [];
        $scope.shopsPage = 1;
        $scope.shopsTotal = 0;
        $scope.pageSize = 10;
        $scope.displayCategories = [];
        Category.listShopCategories()
            .then(function (categories) {
                if (categories.length > 3) {
                    categories = _.filter(categories, function (item, index) {
                        return index < 3
                    });
                }
                $scope.displayCategories = categories;
                $scope.$applyAsync();
            });
        $scope.searchShops = function (keyword) {
            if (!keyword) {
                return;
            }
            $location.path('/search-shops/' + keyword);
        };
        $scope.showPrevPageShops = function() {
            $scope.shopsPage -= 1;
            $scope.loadShops();
        };
        $scope.showNextPageShops = function() {
            $scope.shopsPage += 1;
            $scope.loadShops();
        };
        $scope.shopsPagesCount = function() {
            return 1 + parseInt(($scope.shopsTotal - 1) / $scope.pageSize);
        };
        $scope.loadShops = function() {
            if($scope.shopsPage < 1) {
                $scope.shopsPage = 1;
            }
            if (!$scope.isHotest) {
                Shop.listShops($scope.categoryId, $scope.shopsPage, $scope.pageSize)
                    .then(function (data) {
                        $scope.shops = data.records;
                        $scope.shopsTotal = data.total;
                        $scope.$apply();
                    }).catch(function (err) {
                        if (err) {
                            alert(err);
                        }
                    });
            } else {
                Shop.getHotestShops(20)
                    .then(function (records) {
                        $scope.shops = records;
                        $scope.shopsTotal = records.length;
                        $scope.$apply();
                    }).catch(function (err) {
                        if (err) {
                            alert(err);
                        }
                    });
            }
        };
        $scope.loadShops();
        $(".ctrl-up").hide();
        setInterval(function () {
            var $container = $(".container.content");
            if ($container.length > 0) {
                if ($container.scrollTop() > 0) {
                    $(".ctrl-up").show();
                } else {
                    $(".ctrl-up").hide();
                }
            }
        }, 300);
    }]);